package com.dongly;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.ThreadLocalRandom;

public class SortTestHelper {

    public static Integer[] generateRandomArray(int len, int rangeL, int rangeR) {
        ThreadLocalRandom random = ThreadLocalRandom.current();
        Integer[] arr = new Integer[len];
        for (Integer i = 0; i < len; i++) {
            arr[i] = (random.nextInt(1, 100) % (rangeR - rangeL)) + rangeL;
        }
        return arr;
    }

    public static void printArray(Object[] arr) {
        for (int i = 0, len = arr.length; i < len; i++) {
            System.out.printf("%s ",arr[i]);
        }
        System.out.println();
    }

    public static boolean isSorted(Comparable[] comparables) {
        for (int i = 0,len = comparables.length - 1; i < len; i++) {
            if (comparables[i].compareTo(comparables[i + 1]) > 0) {
                return false;
            }
        }
        return true;
    }

    public static void testSort(Sort sort, Comparable[] comparables) {
        Instant start = Instant.now();
        sort.sort(comparables);
        Instant end = Instant.now();
        assert isSorted(comparables);
        System.out.printf("\n %s : 总耗时[%s]ms \n", sort.getClass(), Duration.between(start, end).toMillis());
    }
}
